/*-
 * Copyright (c) 2009 Jonathan Anderson
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/capability.h>

#include <err.h>
#include <fcntl.h>
#include <stdio.h> // TODO: tmp
#include <stdlib.h>
#include <unistd.h> // TODO: tmp

#include <libcapsicum.h>


struct lc_fdlist* setupFontConfig()
{
	struct File
	{
		const char *subsystem;
		const char *classname;
		const char *filename;
		const char *name;
		int flags;
		cap_rights_t rights;
	};


	cap_rights_t BASE_CAPS
		= CAP_FCNTL | CAP_FSTAT | CAP_FSTATFS | CAP_LOOKUP | CAP_ATBASE;

	cap_rights_t READ_CAPS = BASE_CAPS | CAP_READ | CAP_SEEK;
	cap_rights_t WRITE_CAPS = READ_CAPS | CAP_WRITE;


	struct File files[] = 
	{
		// config directories
		{
			"org.fontconfig", "ConfigDirectory",
			"/home/jon/.fontconfig",
			"~/.fontconfig",//"[Personal Font Configuration Directory]",
			O_RDONLY, READ_CAPS
		},
		{
			"org.fontconfig", "ConfigDirectory",
			"/usr/local/etc/fonts",
			"/usr/local/etc/fonts",//"[System Font Configuration Directory]",
			O_RDONLY, READ_CAPS
		},

		// cache directories (must have real names, as their paths are
		// referenced in font config files)
		{
			"org.fontconfig", "CacheDirectory",
	                 "/home/jon/.fontconfig",
			 "/home/jon/.fontconfig",
			 O_RDONLY, READ_CAPS
		},

		{
			"org.fontconfig", "CacheDirectory",
	                 "/var/db/fontconfig", 
			 "/var/db/fontconfig",
			 O_RDONLY, READ_CAPS
		},

		// font directories (must have real names, as their paths are
		// referenced in font config files)
		{
			"org.fontconfig", "FontDirectory",
	                 "/usr/local/lib/X11/fonts",
			 "/usr/local/lib/X11/fonts",
			 O_RDONLY, READ_CAPS
		},

		{
			"org.freetype", "FontDirectory",
	                 "/usr/local/lib/X11/fonts",
			 "/usr/local/lib/X11/fonts",
			 O_RDONLY, READ_CAPS
		},

		{ NULL, NULL, NULL, NULL, 0, 0 }
	};


	struct lc_fdlist *fdlist = lc_fdlist_new();
	for(int i = 0; files[i].name; i++)
	{
		struct File f = files[i];
		int fd = open(f.filename, f.flags);
		if(fd < 0) err(-1, "Error in open('%s', %d)", f.filename, f.flags);

		if(lc_fdlist_addcap(fdlist,	
			f.subsystem, f.classname, f.name, fd, f.rights) < 0)
			err(-1, "Error in lc_fdlist_addcap('%s')", f.name);
	}

	setenv("FONTCONFIG_PATH", "~/.fontconfig:/usr/local/etc/fonts", 1);
	//setenv("FC_DEBUG", "1040", 1);

	return fdlist;
}

